package ru.tatintec.gwttest.client.view;

import java.util.Date;
import ru.tatintec.gwttest.shared.Employee;
import ru.tatintec.gwttest.shared.Employee.Sex;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DatePicker;

public class CreateEditDialog extends DialogBox {
	
	private Button okButton = new Button("Ok");
	private Button cancelButton = new Button("Cancel");

	private TextBox fnTextBox = new TextBox();
	private TextBox lnTextBox = new TextBox();
	private TextBox mnTextBox = new TextBox();

	private RadioButton sexMaleRadio = new RadioButton("sex", "Male");
	private RadioButton sexFemaleRadio = new RadioButton("sex", "Female");
	
	private DateBox birthdayBox = new DateBox(new DatePicker(), new Date(), new DateBox.DefaultFormat(DateTimeFormat.getFormat("dd.MM.yyyy")));
	private DateBox employmentBox = new DateBox(new DatePicker(), new Date(), new DateBox.DefaultFormat(DateTimeFormat.getFormat("dd.MM.yyyy")));
	
	public CreateEditDialog(String caption) {
		super();
		setHTML(caption);

		cancelButton.addClickHandler(closeHandler);
		Grid grid = new Grid(7, 2);

		grid.setText(0, 0, "Last name");
		grid.setText(1, 0, "First name");
		grid.setText(2, 0, "Middle name");
		grid.setText(3, 0, "Sex");
		grid.setText(4, 0, "Birthday");
		grid.setText(5, 0, "Employment");

		grid.setWidget(0, 1, lnTextBox);
		grid.setWidget(1, 1, fnTextBox);
		grid.setWidget(2, 1, mnTextBox);
		HorizontalPanel hpSex = new HorizontalPanel();
		hpSex.add(sexMaleRadio);
		hpSex.add(sexFemaleRadio);
		grid.setWidget(3, 1, hpSex);
		grid.setWidget(4, 1, birthdayBox);
		grid.setWidget(5, 1, employmentBox);

		HorizontalPanel hp = new HorizontalPanel();
		hp.add(okButton);
		hp.add(cancelButton);
		
		VerticalPanel vp = new VerticalPanel();
		vp.add(grid);
		vp.add(hp);
		setWidget(vp);
	}
	
	
	public void initEditDialog(Employee employee) {
		fnTextBox.setText(employee.getFirstName());
		lnTextBox.setText(employee.getLastName());
		mnTextBox.setText(employee.getMiddleName());
		
		if (employee.getSex().equals(Sex.FEMALE)) {
			sexFemaleRadio.setValue(true);
		} else if (employee.getSex().equals(Sex.MALE)) {
			sexMaleRadio.setValue(true);
		}
		birthdayBox.setValue(employee.getBirthday());
		employmentBox.setValue(employee.getEmploymentDate());
	}


	ClickHandler closeHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			hide();
		}
	};
	
	public void setOkHandler(ClickHandler clickHandler) {
		okButton.addClickHandler(clickHandler);
	}


	public DateBox getBirthdayBox() {
		return birthdayBox;
	}


	public void setBirthdayBox(DateBox birthdayBox) {
		this.birthdayBox = birthdayBox;
	}


	public TextBox getFnTextBox() {
		return fnTextBox;
	}


	public TextBox getLnTextBox() {
		return lnTextBox;
	}


	public TextBox getMnTextBox() {
		return mnTextBox;
	}


	public RadioButton getSexMaleRadio() {
		return sexMaleRadio;
	}


	public RadioButton getSexFemaleRadio() {
		return sexFemaleRadio;
	}


	public DateBox getEmploymentBox() {
		return employmentBox;
	}
	
}
